package com.zeta.demo.mq.common.config;

import cn.dev33.satoken.stp.StpInterface;
import com.zeta.api.client.auth.IAuthApi;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import org.zetaframework.core.constants.RedisKeyConstants;

import java.util.List;

/**
 * 权限数据源加载接口 实现类
 *
 * @author gcc
 */
@Component
@RequiredArgsConstructor
public class StpInterfaceImpl implements StpInterface {

    private final IAuthApi authApi;

    /**
     * 返回指定账号id所拥有的权限码集合
     *
     * @param loginId   账号id
     * @param loginType 账号类型
     * @return 该账号id具有的权限码集合
     */
    @Cacheable(value = RedisKeyConstants.USER_PERMISSION_KEY, key = "#p0", unless = "#result.isEmpty()")
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        return authApi.permissions(loginId, loginType);
    }

    /**
     * 返回指定账号id所拥有的角色标识集合
     *
     * @param loginId   账号id
     * @param loginType 账号类型
     * @return 该账号id具有的角色标识集合
     */
    @Cacheable(value = RedisKeyConstants.USER_ROLE_KEY, key = "#p0", unless = "#result.isEmpty()")
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        return authApi.roles(loginId, loginType);
    }
}
